//
//  WarningView.m
//  MDC
//
//  Created by 123456 on 16/7/9.
//  Copyright © 2016年 YTYangK. All rights reserved.
//

#import "WarningView.h"
#import "AFNetworking.h"
#import "WarningModel.h"
#import "WarningViewModel.h"

@interface WarningView()

@property (strong, nonatomic) NSMutableArray *min;
@property (strong, nonatomic) NSMutableArray *hour;


@end

@implementation WarningView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
    }
    return self;
}

- (NSMutableArray *)min {
    if (!_min) {
        _min = [NSMutableArray array];
    }
    return _min;
}


- (NSMutableArray *)hour {
    if (!_hour) {
        _hour = [NSMutableArray array];
    }
    return _hour;
}

#pragma mark -初始化可以在里写
- (void)awakeFromNib {

    self.SetUpview.layer.masksToBounds = YES;
    self.SetUpview.layer.cornerRadius = 6.0;
    self.SetUpview.layer.borderWidth = 1.0;
    self.SetUpview.layer.borderColor = [[UIColor clearColor] CGColor];

    [self Adaptation];

    for (int i = 0; i<=59; i++) {
        NSString *str =  i < 10 ? [NSString stringWithFormat:@"0%d",i] : [NSString stringWithFormat:@"%d",i];
        [self.min addObject:str];
    }

    for (int i = 0; i<=23; i++) {
        NSString *str =  i < 10 ? [NSString stringWithFormat:@"0%d",i] : [NSString stringWithFormat:@"%d",i];
        [self.hour addObject:str];
    }
}

// 返回2表明该控件只包含2列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 2;
}


// 数据源的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    //判断列
    if (component==0) {
        return [self.hour count];
    }else{
    return [self.min count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        return [self.hour objectAtIndex:row];
    }else{
        return [self.min objectAtIndex:row];
    }
}


// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    
    NSInteger resultOne = [pickerView selectedRowInComponent:0];
    NSInteger resultTwo = [pickerView selectedRowInComponent:1];
    

    
    if ([self.hour[resultOne] integerValue] >=1&& [self.min[resultTwo] integerValue]>0) {
        
        NSString *promptLanguage = [NSString stringWithFormat:@" %ld小时%ld分钟 后报警",(long)[self.hour[resultOne] integerValue] ,(long)[self.min[resultTwo] integerValue]];
        
        NSRange range = [promptLanguage rangeOfString:@"后"];
        
        NSString *str = [promptLanguage substringToIndex:range.location];
        NSInteger i = str.length;
        
        NSMutableAttributedString *promptLanguageMut = [[NSMutableAttributedString alloc]initWithString:promptLanguage];
        // 设置 上面标题
        [promptLanguageMut addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, i)];
        self.titlelabel.attributedText = promptLanguageMut;
        

    }else if ([self.hour[resultOne] integerValue]>=1){
        
        NSString *promptLanguage = [NSString stringWithFormat:@" %ld小时 后报警",(long)[self.hour[resultOne] integerValue]];
        NSRange range = [promptLanguage rangeOfString:@"后"];
        
        NSString *str = [promptLanguage substringToIndex:range.location];
        NSInteger i = str.length;

        NSMutableAttributedString *promptLanguageMut = [[NSMutableAttributedString alloc]initWithString:promptLanguage];
        // 设置 上面标题
        [promptLanguageMut addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, i)];
        self.titlelabel.attributedText = promptLanguageMut;

    }else{
        
        NSString *promptLanguage = [NSString stringWithFormat:@" %ld分钟 后报警",(long)[self.min[resultTwo] integerValue]];
        NSRange range = [promptLanguage rangeOfString:@"后"];
        
        NSString *str = [promptLanguage substringToIndex:range.location];
        NSInteger i = str.length;

        NSMutableAttributedString *promptLanguageMut = [[NSMutableAttributedString alloc]initWithString:promptLanguage];
        // 设置 上面标题
        [promptLanguageMut addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, i)];
        self.titlelabel.attributedText = promptLanguageMut;
       
    }
}


//
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *myView = nil;

if (component == 0) {
    
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 50, 30)];

    myView.textAlignment = NSTextAlignmentCenter;
    
    myView.text = [self.hour objectAtIndex:row];
    
    myView.font = [UIFont systemFontOfSize:17];         //用label来设置字体大小
    
    myView.backgroundColor = [UIColor clearColor];
    
}else if(component ==1){
    
    
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 50, 30)] ;
    
    myView.text =  [self.min objectAtIndex:row];
    
    myView.textAlignment = NSTextAlignmentCenter;
    
    myView.font = [UIFont systemFontOfSize:17];
    
    myView.backgroundColor = [UIColor clearColor];
    
}
return myView;
}

//设置 2个列之间宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView
    widthForComponent:(NSInteger)component
{
    
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")&&component == 0) {
        return 80;
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")&&component ==1){
        return 80;
    }
    

    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")&&component == 0){
        return 85;
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")&&component ==1){
        return 85;
    }
    
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")&&component==0){
        return 95;
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")&&component ==1) {
        return 95;
    }
    
    return component;
}


//上传警告时间
- (IBAction)queding:(id)sender {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.2 animations:^{
            [self removeFromSuperview];

        }];
    }];
   
    
    NSInteger result = [self.datepicker selectedRowInComponent:0];
    
    NSInteger result1 = [self.datepicker selectedRowInComponent:1];
    
    NSInteger zonghm = [self.hour[result] integerValue] *3600 + [self.min[result1]integerValue] *60;
    
    
    if (zonghm == 0) {
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:@"报警时间不可低于1分钟,请重新设置!" toView:nil];
        
    }else{
    //发起上传请求
    [WarningViewModel requestWithUrlForWarning:self andParam:@{@"alarm_criticality":@(zonghm)} success:^(WarningModel *model) {
   
        
        if (model) {
            [MBProgressHUD showSuccess:@"修改成功"];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"JPUSHNOTIFICATION"object:nil];

        }
        
    } failure:^(NSString *error) {
    
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:error toView:nil];
    }];
    
    };
}

- (IBAction)quxiao:(id)sender {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.2 animations:^{
            [self removeFromSuperview];
        }];
    }];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"JPUSHNOTIFICATION1"object:nil];

}

-(void)Adaptation
{
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        self.HWarning.constant = 290;
        self.WWarning.constant = 240;
        self.houtop.constant = -122;
        self.houw.constant = -130;
        self.mintop.constant = -122;
        self.minw.constant = -56;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT,@"667.0")) {
        
        self.HWarning.constant = 320;
        self.WWarning.constant = 260;
        self.BWButton.constant = 130;
        self.QWButton.constant = 130;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT,@"736.0")) {
        self.HWarning.constant = 370;
        self.WWarning.constant = 300;
        self.BHButton.constant = 45;
        self.BWButton.constant = 150;
        self.QHButton.constant = 45;
        self.QWButton.constant = 150;
        self.backvIew.constant = 46;
        self.titlelabel.font = [UIFont systemFontOfSize:16.f];
        self.houtop.constant = -156;
        self.houw.constant = -157;
        self.mintop.constant = -156;
        self.minw.constant = -72;
    }
}




@end
